package com.wing.cabinet.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.StorageInventoryCheckDetail;
import com.wing.cabinet.model.response.StorageInventoryCheckDetailVO;
import com.wing.cabinet.service.StorageInventoryCheckDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.StorageInventoryCheckDetailForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-盘验明细管理", tags = "盘验明细管理")
@RestController("appStorageInventoryCheckDetailController")
@RequestMapping("cabinet/app/storageInventoryCheckDetail")
public class StorageInventoryCheckDetailController {

    @Autowired
    private StorageInventoryCheckDetailService storageInventoryCheckDetailService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询任务明细", notes = "查询")
    @GetMapping("/list")
    public PageResult<StorageInventoryCheckDetailVO> list(
            @RequestParam(name = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", defaultValue = "desc", required = false) String order
            ,Long checkId
    ) {
        Page<StorageInventoryCheckDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        params.put("checkId",checkId);
        Page<StorageInventoryCheckDetailVO> list = storageInventoryCheckDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long storageInventoryCheckDetailId) {
        StorageInventoryCheckDetailVO storageInventoryCheckDetailVO = storageInventoryCheckDetailService.get4appById(storageInventoryCheckDetailId);
        if (storageInventoryCheckDetailVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), storageInventoryCheckDetailVO);
    }

    @PostMapping()
    public JsonResult add(@Validated StorageInventoryCheckDetailForm storageInventoryCheckDetailForm) {
        if (storageInventoryCheckDetailService.add(storageInventoryCheckDetailForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
